package com.tnovoselec.android.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.ViewSwitcher.ViewFactory;

import com.tnovoselec.android.ui.MyViewPager.OnPagerAdapterChangedListener;


public class MyViewPagerNumberIndicator extends TextSwitcher implements OnPageChangeListener, OnPagerAdapterChangedListener, ViewFactory {

    private MyViewPager nthViewPager;
    private PagerAdapter mAdapter;

    private int nthViewPagerId;
    private int textViewLayoutId;
    private boolean hideIfSingle;
    private String format;
    private int prevPosition = -1;

    private Animation animSlideInLeft;
    private Animation animSlideInRight;
    private Animation animSlideOutLeft;
    private Animation animSlideOutRight;

    public MyViewPagerNumberIndicator(Context context) {
	this(context, null);
    }

    public MyViewPagerNumberIndicator(Context context, AttributeSet attrs) {
	super(context, attrs);

	try {
	    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPagerNumberIndicator);
	    nthViewPagerId = a.getResourceId(R.styleable.MyViewPagerNumberIndicator_myViewPagerId, View.NO_ID);
	    textViewLayoutId = a.getResourceId(R.styleable.MyViewPagerNumberIndicator_textViewLayout, R.layout.view_pager_text_indicator_textview_default);
	    hideIfSingle = a.getBoolean(R.styleable.MyViewPagerNumberIndicator_hideIfSingle, true);
	    a.recycle();

	} catch (Exception e) {
	}

	if (TextUtils.isEmpty(format)) {
	    format = "%s / %s";
	}

	animSlideInLeft = AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_left);
	animSlideInRight = AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_right);
	animSlideOutLeft = AnimationUtils.loadAnimation(getContext(), R.anim.slide_out_left);
	animSlideOutRight = AnimationUtils.loadAnimation(getContext(), R.anim.slide_out_right);

	setFactory(this);
    }

    @Override
    protected void onAttachedToWindow() {
	super.onAttachedToWindow();

	if (nthViewPagerId != View.NO_ID) {
	    nthViewPager = (MyViewPager) getRootView().findViewById(nthViewPagerId);
	    setNthViewPager(nthViewPager);
	}
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
	if (position > prevPosition) {
	    setInAnimation(animSlideInRight);
	    setOutAnimation(animSlideOutLeft);
	} else {
	    setInAnimation(animSlideInLeft);
	    setOutAnimation(animSlideOutRight);
	}
	prevPosition = position;
	setText(String.format(format, position + 1, mAdapter.getCount()));
    }

    @Override
    public void onPagerAdapterChanged(MyViewPager viewPager) {
	setAdapter(viewPager.getAdapter());
    }

    public void setAdapter(PagerAdapter adapter) {
	mAdapter = adapter;

	if (mAdapter != null) {
	    if (hideIfSingle && mAdapter.getCount() <= 1) {
		setVisibility(View.GONE);
	    } else {
		setVisibility(View.VISIBLE);
		onPageSelected(nthViewPager.getCurrentItem());
	    }
	}
    }

    /**
     * set ViewPager. adapter is automatically set if instance of
     * INTHViewPagerTabAdapter
     * 
     * @param viewPager
     */
    public void setNthViewPager(MyViewPager viewPager) {
	nthViewPager = viewPager;
	if (nthViewPager != null) {
	    nthViewPager.addOnPageChangeListener(this);
	    nthViewPager.addOnPagerAdapterChangedListeners(this);
	    onPagerAdapterChanged(nthViewPager);
	}
    }

    public void setFormat(String format) {
	this.format = format;
	onPageSelected(nthViewPager.getCurrentItem());
    }

    /**
     * Override this method for providing custom TextView
     */
    @Override
    public View makeView() {
	final View view = inflate(getContext(), textViewLayoutId, null);
	return view;
    }

}
